const Router = require('koa-router');
const db = require("../dbs/db");
const jwt = require("jsonwebtoken");
const TopCategory_Controller = require('../controllers/TopCategory');
const SecCategory_Controller = require('../controllers/SecCategory');
const Article_Controller = require('../controllers/Article');
const Item_Controller = require('../controllers/Item');
const Hero_Controller = require('../controllers/Hero');
const Ad_Controller = require('../controllers/Ad');
const AdminUser_Controller = require('../controllers/AdminUser');
const multer = require('koa-multer');
const upload = multer({ dest: __dirname + '../../public/uploads' });
const router = new Router({
  prefix: '/admin'
});

//toplist接口

router.post('/topcreate', TopCategory_Controller.create);
router.get('/topdetail/:id', TopCategory_Controller.detail);
router.post('/topupdate/:id', TopCategory_Controller.update);
router.post('/topdelete/:id', TopCategory_Controller.delete);
router.post('/toplist', TopCategory_Controller.list);

//seclist接口

router.post('/seccreate', SecCategory_Controller.create);
router.get('/secdetail/:id', SecCategory_Controller.detail);
router.post('/secupdate/:id', SecCategory_Controller.update);
router.post('/secdelete/:id', SecCategory_Controller.delete);
router.post('/seclist', SecCategory_Controller.list);


//Article接口

router.post('/articlecreate', Article_Controller.create);
router.get('/articledetail/:id', Article_Controller.detail);
router.post('/articleupdate/:id', Article_Controller.update);
router.post('/articledelete/:id', Article_Controller.delete);
router.post('/articlelist', Article_Controller.list);


//AdminUser接口

router.post('/AdminUser/create', AdminUser_Controller.create);
router.get('/AdminUser/detail', AdminUser_Controller.detail);
router.post('/AdminUser/update/:id', AdminUser_Controller.update);
router.post('/AdminUser/delete/:id', AdminUser_Controller.delete);
router.post('/AdminUser/list', AdminUser_Controller.list);
router.post('/login', AdminUser_Controller.login);

//Ad接口

router.post('/adcreate', Ad_Controller.create);
router.get('/addetail/:id', Ad_Controller.detail);
router.post('/adupdate/:id', Ad_Controller.update);
router.post('/addelete/:id', Ad_Controller.delete);
router.post('/adlist', Ad_Controller.list);

//item接口

router.post('/upload', upload.single('file'), async (ctx, next) => {
  const token = String(ctx.request.headers.authorization || '').split(' ').pop()
  if (!token) {
    ctx.response.status = 401;
    ctx.body = {
      code: 401,
      msg: '请先登录',
    }
    return
  }
  const { id } = jwt.verify(token, 'glory-of-kings')
  if (!id) {
    ctx.response.status = 401;
    ctx.body = {
      code: 401,
      msg: '请先登录',
    }
    return
  }
  ctx.req.user = await db.AdminUser.findOne({
    where: { id: id }
  })
  if (!ctx.req.user) {
    ctx.response.status = 401;
    ctx.body = {
      code: 401,
      msg: '请先登录',
    }
    return
  }
  await next()
  const file = ctx.req.file
  file.url = `http://localhost:3030/uploads/${file.filename}`
  ctx.response.status = 200;
  ctx.body = {
    file
  }
})
router.post('/itemcreate', Item_Controller.create);
router.get('/itemdetail/:id', Item_Controller.detail);
router.post('/itemupdate/:id', Item_Controller.update);
router.post('/itemdelete/:id', Item_Controller.delete);
router.post('/itemlist', Item_Controller.list);

//hero接口

router.post('/upload', upload.single('file'), async (ctx, next) => {
  const token = String(ctx.request.headers.authorization || '').split(' ').pop()
  if (!token) {
    ctx.response.status = 401;
    ctx.body = {
      code: 401,
      msg: '请先登录',
    }
    return
  }
  const { id } = jwt.verify(token, 'glory-of-kings')
  if (!id) {
    ctx.response.status = 401;
    ctx.body = {
      code: 401,
      msg: '请先登录',
    }
    return
  }
  ctx.req.user = await db.AdminUser.findOne({
    where: { id: id }
  })
  if (!ctx.req.user) {
    ctx.response.status = 401;
    ctx.body = {
      code: 401,
      msg: '请先登录',
    }
    return
  }
  await next()
  const file = ctx.req.file
  file.url = `http://localhost:3030/uploads/${file.filename}`
  ctx.response.status = 200;
  ctx.body = {
    file
  }
})
router.post('/herocreate', Hero_Controller.create);
router.get('/herodetail/:id', Hero_Controller.detail);
router.post('/heroupdate/:id', Hero_Controller.update);
router.post('/herodelete/:id', Hero_Controller.delete);
router.post('/herolist', Hero_Controller.list);

module.exports = router